package makai.image;

/**
 * An alpha map to be applied to other Images.
 * 
 * @author Krechevskoy
 * @version 1.0
 */
public class MakaiImageMask extends MakaiImage {
    
    /**
     * Constructs a new MakaiImageMask with the given dimensions
     */
    public MakaiImageMask(int width, int height) {
        super(width, height, TYPE_BYTE_GRAY);
    }

    /**
     * Constructs a new MakaiImageMask with the dimensions of the given image.
     * 
     * NOTE: The image contents are not used or copied!
     */
    public MakaiImageMask(MakaiImage image) {
        this(image.getWidth(), image.getHeight());
    }
    
    /**
     * Applies this mask to the given Image by modifying it's alpha channel.
     * 
     * NOTE: This method actually modifies the given Image.
     * 
     * @param image The image to apply this mask onto
     * 
     * @return The given Image with the mask applied
     */
    public MakaiImage applyMask(MakaiImage image){
        int[] imagePixels = image.getRGB(0, 0, getWidth(), getHeight(), null, 0, getWidth());
        int[] maskPixels  = getRGB(0, 0, getWidth(), getHeight(), null, 0, getWidth());
        
        for(int i=0; i< imagePixels.length; i++){
            imagePixels[i] = (imagePixels[i]&0xFFFFFF)|((maskPixels[i]&0xFF)<<24);
        }
        
        image.setRGB(0, 0, getWidth(), getHeight(), imagePixels, 0, getWidth());
        
        return image;
    }
    
    /**
     * NOTE: This method does not modify the given Image
     * 
     * @param image The image to apply this mask onto
     * 
     * @return The given Image with the mask applied
     */
    public MakaiImage getMaskedImage(MakaiImage image){
        return applyMask(image.clone());
    }
}
